﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class CageGuideService
    {
        internal static CageGuideOut CageGuideResult(CageGuideIn cageGuideIn)
        {
            //强度验算=IF(D17<E10,"强度OK","强度NO")
            double H4 = 1.1;
            double I4 = 1.4;
            double D4 = cageGuideIn.FJ_Weight;
            double B4 = cageGuideIn.RQ_Weight;
            double E4 = (D4 + B4) / 1000;
            double C9 = cageGuideIn.LoadFactor;
            double F4 = E4 / C9;
            double D14 = H4 * I4 * F4;
            double H7 = cageGuideIn.SliceGap;
            double D19 = D14 * H7 / 5;
            double C7 = cageGuideIn.GD_Thickness;
            double B7 = cageGuideIn.GD_Section;
            double D7 = 0;
            double F7 = 0;
            if (C7 == 8)
            {
                switch (B7)
                {
                    case 120:
                        D7 = 116.1;
                        F7 = 696.6;
                        break;
                    case 140:
                        D7 = 164.8;
                        F7 = 1153.7;
                        break;
                    case 160:
                        D7 = 222;
                        F7 = 1176;
                        break;
                    case 180:
                        D7 = 283;
                        F7 = 2546;
                        break;
                    case 200:
                        D7 = 357;
                        F7 = 3566;
                        break;
                    case 220:
                        D7 = 439;
                        F7 = 4828;
                        break;
                }
            }
            else
            {
                switch (B7)
                {
                    case 200:
                        D7 = 425;
                        F7 = 4251;
                        break;
                    case 220:
                        D7 = 526;
                        F7 = 5782;
                        break;
                }
            }
            double D15 = 0.8 * D14;
            double D20 = D15 * H7 / 5;
            double E7 = D7;
            double D17 = (D19 / D7 + D20 / E7) * 1000000;
            string D10 = cageGuideIn.SteelMaterial;
            double E10 = D10 == "Q235" ? 215 : 310;
            //刚度验算=IF(D24<1/400,"挠度OK","挠度NO")
            double F10 = 206000;
            double D22 = D14 * Math.Pow(H7, 3) / (48 * F10 * F7 / 100000000);
            double D24 = D22 / H7;
            //强度验算=IF(D30<E10,"强度OK","强度NO")
            double C10 = cageGuideIn.DC_GDGS;
            double D16 = 0.25 * D14;
            double D29 = D16;
            double G26 = cageGuideIn.A;
            double G7 = cageGuideIn.L0;
            double I26 = G7 - G26;
            double G32 = cageGuideIn.A2;
            double P9 = C10 == 1 ? D29 * G26 * I26 / G7 * 1.05 : 0;
            double P13 = C10 == 2 ? D29 * (G26 - G32) * (I26 + G32) / G7 * 1.05 : 0;
            double D32 = C10 == 1 ? P9 : P13;
            double G10 = 0;
            double H10 = 0;
            double H11 = 0;
            double I10 = 0;
            double F11 = 0;
            string I8 = cageGuideIn.I_Guide;
            switch (I8)
            {
                case "I20b":
                    G10 = 250;
                    H10 = 33.1;
                    H11 = 169;
                    I10 = 2500;
                    F11 = 0.311;
                    break;
                case "I22b":
                    G10 = 325;
                    H10 = 42.7;
                    H11 = 239;
                    I10 = 3570;
                    F11 = 0.365;
                    break;
                case "I25b":
                    G10 = 423;
                    H10 = 52.4;
                    H11 = 309;
                    I10 = 5280;
                    F11 = 0.42;
                    break;
                case "I28b":
                    G10 = 534;
                    H10 = 61.2;
                    H11 = 379;
                    I10 = 7480;
                    F11 = 0.479;
                    break;
                case "I32b":
                    G10 = 726;
                    H10 = 76;
                    H11 = 502;
                    I10 = 11600;
                    F11 = 0.577;
                    break;
                case "I36b":
                    G10 = 919;
                    H10 = 84.3;
                    H11 = 582;
                    I10 = 16500;
                    F11 = 0.657;
                    break;
                case "I40b":
                    G10 = 1140;
                    H10 = 96.2;
                    H11 = 692;
                    I10 = 22800;
                    F11 = 0.739;
                    break;
                case "I45b":
                    G10 = 1500;
                    H10 = 118;
                    H11 = 894;
                    I10 = 33800;
                    F11 = 0.875;
                    break;
            }
            string I29 = cageGuideIn.LateralBracing;
            double D27 = D14;
            double G30 = cageGuideIn.A1;
            double I30 = cageGuideIn.B1;
            double N16 = G30 < G32 ? D27 * (G26 - G32) * (G32 - G30) / (G26 - I30) * 1.05 : 0;
            double P16 = G30 > G32 ? D27 * (G30 - G32) * (I26 + G32) * 1.05 / (I26 + G30) : 0;
            double P11 = C10 == 1 ? D27 * G26 * I26 / G7 * 1.05 : 0;
            double P15 = C10 == 2 ? D27 * (G26 - G32) * (I26 + G32) / G7 * 1.05 : 0;
            double P10 = C10 == 1 ? D27 * G30 * I30 / (G30 + I30) * 1.05 : 0;
            double P14 = C10 == 2 ? (G30 < G32 ? N16 : P16) : 0;
            double D33 = I29 == "N" ? (C10 == 1 ? P11 : P15) : (C10 == 1 ? P10 : P14);
            double D30 = (D32 / G10 + D33 / H10) * 1000000;

            CageGuideOut cageGuideOut = new CageGuideOut();

            cageGuideOut.B = I26;
            cageGuideOut.FGGD_GD = D17 < E10 ? "强度OK" : "强度NO";
            cageGuideOut.FGGD_GD_Calculated = D17;
            cageGuideOut.FGGD_GD_Admissible = E10;

            cageGuideOut.FGGD_QD = D24 < 0.0025 ? "挠度OK" : "挠度NO";
            cageGuideOut.FGGD_QD_Calculated = D24;

            cageGuideOut.GDL_GD = D30 < E10 ? "强度OK" : "强度NO";
            cageGuideOut.GDL_GD_Calculated = D30;
            cageGuideOut.GDL_GD_Admissible = E10;
            //挠度验算=IF(D37<1/400,"挠度OK","挠度NO")
            double D35 = I29 == "N" ? D27 * I26 / (3 * F10 * H11 * G7) * Math.Pow((Math.Pow(((Math.Pow(G26, 2) + 2 * G26 * I26) / 3), 3)), 0.5) * 100000000 : 0;
            double D36 = I29 == "Y" ? D29 * I26 / (3 * F10 * I10 * G7) * Math.Pow((Math.Pow(((Math.Pow(G26, 2) + 2 * G26 * I26) / 3), 3)), 0.5) * 100000000 + 5 * F11 * Math.Pow(G7, 4) / (384 * F10 * I10) * 100000 : 0;
            double D37 = I29 == "N" ? D35 / G7 : D36 / G7;
            cageGuideOut.GDL_ND = D37 < 0.0025 ? "挠度OK" : "挠度NO";
            cageGuideOut.GDL_ND_Calculated = D37;
            //是否设侧向支撑=IF(I29="y","已设侧向支撑",IF(E10-M18<M19,"必须设侧向支撑","强度OK，核挠度"))
            double M18 = D32 / G10 * 1000000;
            double M19 = D33 / H10 * 1000000;
            cageGuideOut.LateralBracing = I29 == "Y" ? "已设侧向支撑" : (E10 - M18 < M19 ? "必须设侧向支撑" : "强度OK，核挠度");

            return cageGuideOut;
        }
    }
}
